﻿Public Partial Class frm_Transection
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "TRANSACTION "
            LoadTransection("", "", "A")
        End If
    End Sub

    Private Sub LoadTransection(ByVal Customer As String, ByVal Doc_Date As String, ByVal Type_Pay As String)
        ViewState("Transection") = clsGet.Get_Transection(Customer, Doc_Date, Type_Pay)
        If CType(ViewState("Transection"), DataTable).Rows.Count = 0 Then
            lbAlert.Visible = True
            gridItem.DataSource = Nothing
            gridItem.DataBind()
            lbAlert.Text = "ไม่พบรายการชำระเงิน"
        Else
            lbAlert.Visible = False
            gridItem.DataSource = ViewState("Transection")
            gridItem.DataBind()
        End If
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridItem.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridItem.PageIndex = 0
            Case "prev"
                If (gridItem.PageIndex <> 0) Then
                    gridItem.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridItem.PageIndex <> gridItem.PageCount) Then
                    gridItem.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridItem.PageIndex = gridItem.PageCount
        End Select
        gridItem.DataSource = ViewState("Transection")
        gridItem.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridItem.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridItem.PageIndex = ddlPage.SelectedIndex
        gridItem.DataSource = ViewState("Transection")
        gridItem.DataBind()
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Try
                Dim Vat As String = gridItem.DataKeys(e.Row.RowIndex).Values("Vat") & ""
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Vat") & "" = "Vat") Then
                    Vat = 0.0
                End If
                e.Row.Cells(5).Text = FormatNumber((CDbl(gridItem.DataKeys(e.Row.RowIndex).Values("Amount")) + CDbl(gridItem.DataKeys(e.Row.RowIndex).Values("Vat"))) - CDbl(gridItem.DataKeys(e.Row.RowIndex).Values("WHT")), 2)
                e.Row.Cells(6).Text = FormatNumber(e.Row.Cells(6).Text, 2)
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Type_Pay") & "" = "E") Then
                    e.Row.Cells(7).Text = "E-Payment"
                Else
                    e.Row.Cells(7).Text = "Manual"
                End If
            Catch ex As Exception

            End Try
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridItem.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridItem.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridItem.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridItem.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtCustomer.Text = String.Empty
        txtDocDate.Text = String.Empty
        ddlTypePay.ClearSelection()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim d_Date As String = ""
        Try
            d_Date = CDate(txtDocDate.Text).ToString("yyyyMMdd")
        Catch ex As Exception

        End Try
        LoadTransection(txtCustomer.Text, d_Date, ddlTypePay.SelectedValue)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class